const request=require("../../wechat-lib/request")
const login=require('../../wechat-lib/login');
let user=require('../../wechat-lib/user');
const common=require('../../wechat-lib/common');
Page({
  mixins: [require('../../wechat-lib/themeChanged'),require('../../wechat-lib/openId'),require('../../wechat-lib/validForm')],
  data: {
    rules: [{
      name: 'userNickName',
      rules: [{required: true, message: '昵称不能为空'},{rangelength:[3,10],message:'标题长度不合适'}]
    },
    {
      name:"birthDate",
      rules:[{required:true,message:'生日不能为空'}]
    },{
      name: 'mobile',
      rules: [{required: true, message: '联系方式不能为空'},{ mobile:true,message:'方便提供优质服务'}]
    },

    
    ],
    formData: {
    },
    disableSubmit:"disable"
  },
  onLoad:function(){
    let _that=this;
    user.getNickName('提供个性化服务',false).then(
      success=>{
        console.info(success)
        _that.setData({
          [`formData.userNickName`]:success
        })
      },
      err=>{
        console.error(err)
        common.showErrorMsg(common.error("请授权，以获得更多服务\n"+err.errorMsg,err.errorCode))
      }
    )
  },
  getNickName(){
    const _that=this;
    user.getNickName('提供个性化服务',true).then(
      success=>{
        console.info(success)
        _that.setData({
          [`formData.userNickName`]:success
        })
      },
      err=>{
        console.error(err)
        common.showErrorMsg(common.error("请授权，以获得更多服务\n"+err.errorMsg,err.errorCode))
      }
    )
  },

reportChange: function (e) {
  const {field} = e.currentTarget.dataset
  this.setData({
      [`${field}`]: e.detail.value,
      [`formData.${field}`]: e.detail.value
  });
},
radioChange: function (e) {
  console.log('radio发生change事件，携带value值为：', e.detail.value);

  var radioItems = this.data.radioItems;
  for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
  }

  this.setData({
      radioItems: e.detail.value[0],
      [`formData.radio`]: e.detail.value[0]
  });
},
agreeChange: function (e) {
  console.log('checkbox发生change事件，携带value值为：', e.detail.value);
  const {field} = e.currentTarget.dataset
  this.setData({
      [`${field}`]: e.detail.value,
      [`formData.${field}`]: e.detail.value
  });
},
bindTimeChange: function (e) {
  this.setData({
      time: e.detail.value
  })
},
bindCountryCodeChange: function(e){
  console.log('picker country code 发生选择改变，携带值为', e.detail.value);

  this.setData({
      countryCodeIndex: e.detail.value
  })
},
bindCountryChange: function(e) {
  console.log('picker country 发生选择改变，携带值为', e.detail.value);

  this.setData({
      countryIndex: e.detail.value
  })
},
bindAccountChange: function(e) {
  console.log('picker account 发生选择改变，携带值为', e.detail.value);
  this.setData({
      accountIndex: e.detail.value
  })
},
bindAgreeChange: function (e) {
  this.setData({
      isAgree: !!e.detail.value.length
  });
},
bindsuccess:function(e){
  this.setData({
    disableSubmit:false
  })
},
bindfail:function(e) {
  this.setData({
    disableSubmit:true
  })
},
  submit:async function() {
    if(await this.validForm('#form')){
      login.getJwtPayload().then(success=>{
        console.log(success)
        request.yunrequest("user","update",this.data.formData).then(
          success=>{

          },
          err=>{
            common.showErrorMsg(err)
            // console.log(err)
          }
        )
      },err=>{
        common.showErrorMsg(err)
      })
    }
  }
})